#define LOG_TAG "uart_recv"
#include <COMMON_HEADER/common_header.h>
/*
 * Copyright (c) 2016-2020, Chengdu RST technology co., ltd
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021-01-05     shijiabao       the first version
 */

/* 串口接收消息结构定义*/
struct rx_msg
{
    rt_device_t dev;
    rt_size_t size;
};

/* 消息队列控制块 */
static struct rt_messagequeue rs485_uart_rx_mq;
static struct rt_messagequeue rs232_uart_rx_mq;

/*用于debug串口接收到数据的信号量*/
static struct rt_semaphore debug_rx_sem;

static char msg_pool[1024];

void debug_uart_semaphore_init(void)
{
    /* 初始化信号量 */
    rt_sem_init(&debug_rx_sem, "debug_rx_sem", 0, RT_IPC_FLAG_FIFO);
}

void rs485_uart_messagequeue_init(void)
{
    /* 初始化消息队列 */
    rt_mq_init(&rs485_uart_rx_mq, "rs485_uart_rx_mq",
               msg_pool,                 /* 存放消息的缓冲区 */
               sizeof(struct rx_msg),    /* 一条消息的最大长度 */
               sizeof(msg_pool),         /* 存放消息的缓冲区大小 */
               RT_IPC_FLAG_FIFO);        /* 如果有多个线程等待，按照先来先得到的方法分配消息 */
}

void rs232_uart_messagequeue_init(void)
{
    /* 初始化消息队列 */
    rt_mq_init(&rs232_uart_rx_mq, "rs232_uart_rx_mq",
               msg_pool,                 /* 存放消息的缓冲区 */
               sizeof(struct rx_msg),    /* 一条消息的最大长度 */
               sizeof(msg_pool),         /* 存放消息的缓冲区大小 */
               RT_IPC_FLAG_FIFO);        /* 如果有多个线程等待，按照先来先得到的方法分配消息 */
}


/* 接收数据回调函数 */
rt_err_t debug_uart_input(rt_device_t dev, rt_size_t size)
{
    /* 串口接收到数据后产生中断，调用此回调函数，然后发送接收信号量 */
       if (size > 0)
       {
           rt_sem_release(&debug_rx_sem);
       }
       return RT_EOK;
}

rt_err_t rs485_uart_input(rt_device_t dev, rt_size_t size)
{
    struct rx_msg msg;
    rt_err_t result;
    msg.dev = dev;
    msg.size = size;

    result = rt_mq_send(&rs485_uart_rx_mq, &msg, sizeof(msg));
    if ( result == -RT_EFULL)
    {
        /* 消息队列满 */
        LOG_E("message queue full！\n");
    }
    return result;

}

rt_err_t rs232_uart_input(rt_device_t dev, rt_size_t size)
{
    struct rx_msg msg;
    rt_err_t result;
    msg.dev = dev;
    msg.size = size;

    result = rt_mq_send(&rs232_uart_rx_mq, &msg, sizeof(msg));
    if ( result == -RT_EFULL)
    {
        /* 消息队列满 */
        LOG_E("message queue full！\n");
    }
    return result;
}


/*接收数据处理线程*/
void debug_serial_thread_entry(void *parameter)
{
    char ch;
    char debug_recv_data_buf[2058] = {0};
    int debug_rx_buffer_size = 0;
    while (1)
    {
        /* 从串口读取一个字节的数据，没有读取到则等待接收信号量 */
        while (rt_device_read(debug_serial, -1, &ch, 1) != 1)
        {
            /* 阻塞等待接收信号量，等到信号量后再次读取数据 */
            rt_sem_take(&debug_rx_sem, RT_WAITING_FOREVER);
        }
        debug_recv_data_buf[debug_rx_buffer_size] = ch;
        debug_rx_buffer_size += 1;
        if(debug_rx_buffer_size >= 2058)
        {
            debug_uart_private_protocol_data_handle(debug_recv_data_buf);
            debug_rx_buffer_size = 0;
            led2_twinkle();
        }
    }
}

void rs485_serial_thread_entry(void *parameter)
{
    struct rx_msg msg;
    rt_err_t result;
    rt_uint32_t rx_length;
    char *uart_recv_data = NULL;

    while (1)
    {
        rt_memset(&msg, 0, sizeof(msg));
        /* 从消息队列中读取消息*/
        result = rt_mq_recv(&rs485_uart_rx_mq, &msg, sizeof(msg), RT_WAITING_FOREVER);
        if (result == RT_EOK)
        {
            led2_twinkle();
            uart_recv_data = (char*)rt_malloc(msg.size);
            /* 从串口读取数据*/
            rx_length = rt_device_read(msg.dev, 0, uart_recv_data, msg.size);
            LOG_D("RS485串口接收到数据:%s\n",uart_recv_data);
            LOG_I("RS485串口接收到数据");

#if P_EN==1
     rt_kprintf("RS485_recv:%s\n",uart_recv_data);
    for(int i =0;i<rx_length;i++)
    {
        rt_kprintf("uart_recv_data=%d",uart_recv_data[i]);
    }
    rt_kprintf("\n");
#endif
            //通道1-2，直接透传数据到服务器
            if(lte_data.channel_connect_flag[1] == 1)
            {
               if(!lte_channel_send_data(1, uart_recv_data,rx_length))
               {
                   LOG_I("串口数据上传通道1成功");
               }

            }
            if(lte_data.channel_connect_flag[2] == 1)
            {
               if(!lte_channel_send_data(2, uart_recv_data,rx_length))
               {
                   LOG_I("串口数据上传通道2成功");
               }
            }
            else
            {
               ;
            }
            rt_free(uart_recv_data);
        }
    }
}

void rs232_serial_thread_entry(void *parameter)
{
    struct rx_msg msg;
    rt_err_t result;
    rt_uint32_t rx_length;
    char *uart_recv_data = NULL;

    while (1)
    {
        rt_memset(&msg, 0, sizeof(msg));
        /* 从消息队列中读取消息*/
        result = rt_mq_recv(&rs232_uart_rx_mq, &msg, sizeof(msg), RT_WAITING_FOREVER);
        if (result == RT_EOK)
        {
            led2_twinkle();
            /* 从串口读取数据*/
            uart_recv_data = (char*)rt_malloc(msg.size);
            rx_length = rt_device_read(msg.dev, 0, uart_recv_data, msg.size);
            uart_recv_data[rx_length] = '\0';
            LOG_D("RS232串口接收到数据:%s",uart_recv_data);
            LOG_I("RS232串口接收到数据");
            //通道1-4，直接透传数据到服务器
            if(lte_data.channel_connect_flag[1] == 1)
            {
                if(!lte_channel_send_data(1, uart_recv_data,rx_length))
                {
                    LOG_I("串口数据上传通道1成功");
                }
            }
            if(lte_data.channel_connect_flag[2] == 1)
            {
                if(!lte_channel_send_data(2, uart_recv_data,rx_length))
                {
                    LOG_I("串口数据上传通道1成功");
                }
            }
            else
            {
                 ;
            }
            rt_free(uart_recv_data);
        }

    }
}



